package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.dao.WebAreaDao;
import ysq.xyg.web.model.po.WebArea;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebAreaVo;
import ysq.xyg.web.service.WebAreaService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 业务模块-活动室信息service实现
 *
 * @author shengQiang yu
 * @date 2019-5-15 22:39:09
 */
@Service("webAreaService")
public class WebAreaServiceImpl extends BaseServiceImpl<WebArea> implements WebAreaService {

    @Resource
    private WebAreaDao webAreaDao;

    private WebArea webArea;

    @Autowired
    public void setBaseDao(WebAreaDao webAreaDao) {
        this.baseDao = webAreaDao;
    }


    /**
     * 分页查询
     *
     * @param webAreaVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-5-15 22:39:09
     */
    public ReturnEntity page(WebAreaVo webAreaVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<WebArea> list = leftList(webAreaVo);
        PageInfo<WebArea> page=new PageInfo<WebArea>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webAreaVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-5-15 22:39:09
     */
    public ReturnEntity addOrUpdate(WebAreaVo webAreaVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webAreaVo.getId())){
            webArea=new WebArea(currentUser);
            isUpate=false;
        }else{
            webArea=selectByPrimaryKey(webAreaVo.getId());
            if(ObjectUtil.isNull(webArea)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webArea.setUpdateTime(new Date());
            webArea.setUpdateUser(currentUser.getId());
        }
        webArea.setTitle(webAreaVo.getTitle());
        webArea.setAreaGrade(webAreaVo.getAreaGrade());
        webArea.setUserId(webAreaVo.getUserId());
        webArea.setAddress(webAreaVo.getAddress());
        webArea.setPicture(webAreaVo.getPicture());
        webArea.setLinkUser(webAreaVo.getLinkUser());
        webArea.setLinkPhone(webAreaVo.getLinkPhone());
        webArea.setAreaSize(webAreaVo.getAreaSize());
        webArea.setVolume(webAreaVo.getVolume());
        webArea.setContent(webAreaVo.getContent());
        webArea.setStatus(webAreaVo.getStatus());
        webArea.setSort(webAreaVo.getSort());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webArea);
        }else{
            num=insert(webArea);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-5-15 22:39:09
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebArea webArea=selectByPrimaryKey(id);
        if(webArea!=null){
            webArea.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webArea.setUpdateTime(new Date());
            webArea.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webArea);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webAreaVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-5-15 22:39:09
     */
    public List<WebArea> acceptList(WebAreaVo webAreaVo){
        Example example = new Example(WebArea.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webAreaVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webAreaVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return webAreaDao.selectByExample(example);
    }
    /**
     * 获取集合
     *
     * @param webAreaVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-5-15 22:39:09
     */
    public List<WebArea> leftList(WebAreaVo webAreaVo){
        return webAreaDao.leftList(webAreaVo);
    }
}
